//
//  offer.swift
//  PlayData
//
//  Created by 冯琰琰 on 2022/3/22.
//

import Foundation
class CQueue {

    var stack1 = [Int]()
    var stack2 = [Int]()
    init() {

    }
    
    func appendTail(_ value: Int) {
        stack1.append(value)
    }
    
    func deleteHead() -> Int {
        if stack2.isEmpty {
            while !stack1.isEmpty {
                stack2.append(stack1.removeFirst())
            }
        }
        if stack2.isEmpty {
            return -1
        } else {
            return stack2.removeFirst()
        }
    }
    
    func fib(_ n: Int) -> Int {
        if n < 2 {
            return n
        }
        return fib(n-1) + fib(n-2)
    }
    
    func fib2(_ n: Int) -> Int {
        if n < 2 {
            return n
        }
        if n == 2 {
            return 1
        }
        let e = 1000000007
        // 0 1 1 2 3 5 8 13
        var p = 0
        var q = 1
        var sum = 1
        // 3 4 5 6 7
        for _ in 3...n {
            p = q
            q = sum
            sum = (p + q) % e
        }
        return sum
    }
    
    // n:0 1 2 3 4
    // s:1 1 2 3 5
    
    func numWays(_ n: Int) -> Int {
        if n == 0 {
            return 1
        }
        if n <= 3 {
            return n
        }
        let e = 1000000007
        var p = 1
        var q = 2
        var sum = 3
        for _ in 4...n {
            p = q
            q = sum
            sum = (p + q) % e
        }
        return sum
    }
    
    
    func lengthOfLongestSubstring(_ s: String) -> Int {
        if s.count == 1 {
            return 1
        }
        let string = Array(s)
        var sAr = [String]()
        var max = 0
        for i in 0..<string.count {
            sAr.removeAll()
            for j in i..<string.count {
                let temp = string[j]
                let ss = String(temp)
                
                if sAr.contains(ss) {
                    break
                }
                sAr.append(ss)
                if sAr.count > max {
                    max = sAr.count
                }
            }
        }
        return max
     }

    func reverse(_ x: Int) -> Int {
        var temp = x
        var result = 0
        while (temp != 0) {
            let der = temp % 10
            temp = temp / 10
            result = result * 10 + der
            if result > Int32.max - 1 || result < -Int.max {
                return 0
            }
        }
        
        return result
    }
}
